package org.WhyMCA.WhyMCANews;

/**
@author Danilo '' Riso >> danilo DOT riso AT gmail DOT com
* @author Androidiani Dev Team
* @version 1.0
*/

import org.WhyMCA.Services.WhyMCAService;
import org.WhyMCA.Utils.Global;
import org.WhyMCA.Utils.PreferencesMgr;
import org.WhyMCA.WhyMCANews.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class Settings extends Activity implements OnCheckedChangeListener, OnItemSelectedListener, OnClickListener {

	private Spinner _timechoice;
	private CheckBox _sound;
	private ArrayAdapter _timearray;
	private Long _timemill;
	private Button _savebutt, _cancelbutt;
	private static SharedPreferences _sharpref;
	private static SharedPreferences.Editor _prefed;
	private static Intent _whymcaservice;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Global.setCtx(this);
		setContentView(R.layout.settings);
		
		createElements();
		
	}
		
	private void createElements() {
		
		_timechoice = (Spinner) findViewById(R.id.SpinnerSettings);
    	_timearray = ArrayAdapter.createFromResource( this, R.array.update_time, android.R.layout.simple_spinner_item);
    	_timechoice.setAdapter(_timearray);
    	_sound = (CheckBox) findViewById(R.id.checkBox1);
    	_savebutt = (Button) findViewById(R.id.ButtonSave);
    	_cancelbutt = (Button) findViewById(R.id.ButtonCancel);
       	
    	_sound.setChecked(org.WhyMCA.Utils.PreferencesMgr.getSoundConfig());
    	_timechoice.setOnItemSelectedListener(this);
    	_savebutt.setOnClickListener(this);
    	_cancelbutt.setOnClickListener(this);
    	_sound.setOnCheckedChangeListener(this);
	
    	_sharpref = Global.getCtx().getSharedPreferences("WhyMCA", 0);
    	_prefed = _sharpref.edit();
    	    	    	
    	_timechoice.setSelection((int) _sharpref.getLong("spinpos", 0));
    	    	
	}

	private void createTimeMill(String s) {
		
		if(s.contains("secondi")) {
			
			_timemill = (Long.valueOf(s.substring(0, s.indexOf(" "))) * 1000);
			System.out.println(_timemill);
			
		} else if(s.contains("minuto") || s.contains("minuti")) {
			
			_timemill = ((Long.valueOf(s.substring(0, s.indexOf(" "))) * 60) * 1000);
			System.out.println(_timemill);
			
		} else if(s.contains("ora") || s.contains("ore")) {
			
			_timemill = ((Long.valueOf(s.substring(0, s.indexOf(" "))) * 3600) * 1000);
			System.out.println(_timemill);
			
		}
		
	}
	
	private long getTimeMill() {
		
		return _timemill;
		
	}
	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		
		createTimeMill((String)arg0.getSelectedItem());
		_prefed.putLong("spinpos", arg0.getSelectedItemId());
		_prefed.commit();
						
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		
		//createTimeMill((String)arg0.getSelectedItem());
		
	}

	public static void setWhyMCAService(Intent whymcaservice) {
		
		_whymcaservice = whymcaservice;
		
	}
	
	private Intent getWhyMCAService() {
		
		return _whymcaservice;
		
	}
	
	@Override
	public void onClick(View v) {
		
		switch(v.getId()) {
		
			case R.id.ButtonSave:
		
				_prefed.putLong("TIME", this.getTimeMill());
				_prefed.putBoolean("NOT_SOUND", _sound.isChecked());
				_prefed.commit();
				WhyMCAService.setTimeMill(this.getTimeMill());
				startService(getWhyMCAService());
				this.finish();
				
				break;
				
			case R.id.ButtonCancel:
				
				this.finish();
				
				break;
		
		}
		
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		
		if(isChecked) {
			
			PreferencesMgr.setSoundConfig(true);
			//System.out.println(PreferencesMgr.getGpsOnStart());
			
		} else {
			
			PreferencesMgr.setSoundConfig(false);
			//System.out.println(PreferencesMgr.getGpsOnStart());
			
		}
	
		System.out.println("Checkbox: " + PreferencesMgr.getSoundConfig());
		
	}
	
}
